package com.tianji.promotion.controller;

import com.tianji.common.domain.dto.PageDTO;
import com.tianji.promotion.domain.dto.CouponFormDTO;
import com.tianji.promotion.domain.dto.CouponIssueFormDTO;
import com.tianji.promotion.domain.query.CouponQuery;
import com.tianji.promotion.domain.vo.CouponDetailVO;
import com.tianji.promotion.domain.vo.CouponPageVO;
import com.tianji.promotion.domain.vo.CouponVO;
import com.tianji.promotion.service.CouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 优惠券的规则信息 控制器
 * </p>
 *
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/coupons")
@Api(tags = "优惠券相关接口")
public class CouponController {

    private final CouponService couponService;

    /**
     * 新增优惠券接口
     * @param couponFormDTO
     */
    @PostMapping
    @ApiOperation("新增优惠券接口")
    public void saveCoupon(@RequestBody @Valid CouponFormDTO couponFormDTO) {
        couponService.saveCoupon(couponFormDTO);
    }

    /**
     * 分页查询优惠券
     * @param couponQuery
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询优惠券")
    public PageDTO<CouponPageVO> queryCouponByPage(@RequestBody CouponQuery couponQuery) {
        return couponService.queryCouponByPage(couponQuery);
    }

    /**
     * 发放优惠券接口
     * @param couponIssueFormDTO
     */
    @ApiOperation("发放优惠券接口")
    @PutMapping("/{id}/issue")
    public void beginIssue(@RequestBody @Valid CouponIssueFormDTO couponIssueFormDTO) {
        couponService.beginIssue(couponIssueFormDTO);
    }

    /**
     * 删除优惠券
     * @param id
     */
    @DeleteMapping("/{id}")
    @ApiOperation("删除优惠券")
    public void deleteById(@ApiParam("优惠券id") @PathVariable("id") Long id) {
        couponService.deleteById(id);
    }

    /**
     * 根据id查询优惠券接口
     * @param id
     * @return
     */
    @ApiOperation("根据id查询优惠券接口")
    @GetMapping("/{id}")
    public CouponDetailVO queryCouponById(@ApiParam("优惠券id") @PathVariable("id") Long id){
        return couponService.queryCouponById(id);
    }

    /**
     * 查看发放中的优惠券
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询发放中的优惠券")
    public List<CouponVO> queryIssuingCoupons(){
        return couponService.queryIssuingCoupons();
    }

}
